
var edgesns = edgesns || {};
edgesns.framework = stylefree.framework();
edgesns.ajax = stylefree.ajax({framework:edgesns.framework});


(function(framework){
    var my = {
        key: "/showMyPage",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	stylefree.template.render("#top_contents", "/templates/layout_default.ajp" );
    	that.getAction("/showMyProfile").execute(request);
    	stylefree.memcache.set("user", "currentUserId", null);
    	that.getAction("/showFriendList").execute(request);
    	that.getAction("/showRecentFriendDiaryList").execute(request);
    	//stylefree.template.render("#top_contents .right1", "/templates/recentFriendDiaryList.ajp", "recentFriendDiaryList", {});
    	edgesns.tab.select(0);
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/showUserPage",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	
    	if(stylefree.memcache.get("user", "userId") == request.params.userId) {
        	that.getAction("/showMyPage").execute(request);
        	return;
    	}
    	stylefree.template.render("#top_contents", "/templates/layout_default.ajp" );
    	that.getAction("/showUserProfile").execute(request);
    	that.getAction("/showFriendList").execute(request);
    	stylefree.template.render("#top_contents .left2", "/templates/friendList.ajp", "friendList", {});
    	edgesns.tab.select(0);
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/showDiaryPage",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	stylefree.template.render("#diary_contents", "/templates/layout_default.ajp" );
    	stylefree.template.render("#diary_contents .left1", "/templates/diaryMenu.ajp", "calender", {});
    	stylefree.template.render("#diary_contents .left2", "/templates/calender.ajp", "calender", {});
    	$("#datepicker").datepicker({inline: true});
    	that.getAction("/showDiaryList").execute(request);
    }
})(edgesns.framework);


(function(framework){
    var my = {
        key: "/showMyProfile",
        framework: framework
    };
    var that = stylefree.action(my);
    
    that.execute = function(request){
    	edgesns.ajax.callService("/edgesns/getMyProfile", request.params, function(response) {
    		stylefree.memcache.set("user", "userId", response.contents.userId);
    		stylefree.memcache.set("user", "currentUserId", response.contents.currentUserId);
    		stylefree.memcache.set("user", "name", response.contents.name);
    		stylefree.memcache.set("user", "currentName", response.contents.name);
    		$("#logout").attr("href", response.status.logoutUrl);
    		stylefree.template.render("#top_contents .left1", "/templates/profile.ajp", "profile", response.contents);
    	});
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/showUserProfile",
        framework: framework
    };
    var that = stylefree.action(my);
    
    that.execute = function(request){
    	edgesns.ajax.callService("/edgesns/getUserProfile", request.params, function(response) {
    		stylefree.memcache.set("user", "currentUserId", response.contents.currentUserId);
    		stylefree.memcache.set("user", "currentName", response.contents.name);
    		stylefree.template.render("#top_contents .left1", "/templates/profile.ajp", "profile", response.contents);
        	stylefree.template.render("#top_contents .right1", "/templates/friendStatus.ajp", "friendStatus", response.contents.friendStatus);
        	stylefree.template.render("#top_contents .right2", "/templates/selfIntroduction.ajp", "selfIntroduction", {});
        	stylefree.binding.set("#selfIntroduction", response.contents);
    	});
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/showDiary",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	edgesns.ajax.callService("/edgesns/getDiary", request.params, function(response) {
    		stylefree.template.render("#diary_contents .right1", "/templates/diary.ajp", "diary");
    		stylefree.binding.set("#diary", response.contents);
    	});
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/showDiaryList",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	request.params["currentUserId"] = stylefree.memcache.get("user", "currentUserId");
    	edgesns.ajax.callService("/edgesns/getDiaryList", request.params, function(response) {
    		//alert(response.contents.diaryList.length);
    		stylefree.template.render("#diary_contents .right1", "/templates/diaryList.ajp", "diaryList", response.contents.diaryList);
    	});
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/showFriendList",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	var userId = request.params.userId || stylefree.memcache.get("user", "currentUserId");
    	if(userId !== null && userId !== undefined) {
        	request.params["userId"] = userId;
    	}
    	edgesns.ajax.callService("/edgesns/getFriendList", request.params, function(response) {
    		//alert(response.contents.diaryList.length);
    		stylefree.template.render("#top_contents .left2", "/templates/friendList.ajp", "friendList", response.contents.friendList);
    	});
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/showRecentFriendDiaryList",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	edgesns.ajax.callService("/edgesns/getRecentFriendDiaryList", request.params, function(response) {
    		//alert(response.contents.diaryList.length);
    		stylefree.template.render("#top_contents .right1", "/templates/recentFriendDiaryList.ajp", "recentFriendDiaryList", response.contents.recentFriendDiaryList);
    	});
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/saveProfile",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	edgesns.ajax.callService("/edgesns/saveProfile", request.params, function(response) {
    		if(response.status.code == "0") {
        		stylefree.memcache.set("user", "name", response.contents.name);
        		stylefree.binding.set("#profileEditor", response.contents);
        		alert("保存しました。");
    		} else {
        		alert("保存に失敗しました。");
    		}
    	});
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/saveDiary",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	edgesns.ajax.callService("/edgesns/saveDiary", request.params, function(response) {
        	stylefree.template.render("#diary_contents .right1", "/templates/result.ajp", "diaryList", {});
    	});
    }
})(edgesns.framework);


(function(framework){
    var my = {
        key: "/showProfileEditor",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(options){
    	stylefree.template.render("#config_contents", "/templates/layout_column1.ajp" );
    	edgesns.ajax.callService("/edgesns/getMyProfile", {}, function(response) {
    		stylefree.template.render("#config_contents .center1", "/templates/profileEditor.ajp", "profileEditor", response);
    		stylefree.binding.set("#profileEditor", response.contents);
    	});
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/showDiaryEditor",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(options){
		stylefree.template.render("#diary_contents .right1", "/templates/diaryEditor.ajp", "diaryEditor", {});
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/showSearchPage",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(options){
    	stylefree.template.render("#search_contents", "/templates/layout_column1.ajp" );
		stylefree.template.render("#search_contents .center1", "/templates/userSearchInput.ajp", "userSearchInput");
    	edgesns.ajax.callService("/edgesns/searchUser", {}, function(response) {
    		stylefree.template.render("#search_contents .center2", "/templates/userSearchResult.ajp", "userSearchResult", response.contents.searchResult);
    	});
    }
})(edgesns.framework);


(function(framework){
    var my = {
        key: "/registerFriend",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	edgesns.ajax.callService("/edgesns/registerFriend", request.params, function(response) {
    		alert("登録しました。");
			edgesns.framework.process("/showUserPage?userId=" + stylefree.memcache.get("user", "currentUserId"));
    	});
    }
})(edgesns.framework);

(function(framework){
    var my = {
        key: "/releaseFriend",
        framework: framework
    };
    var that = stylefree.action(my);
    that.execute = function(request){
    	edgesns.ajax.callService("/edgesns/releaseFriend", request.params, function(response) {
    		alert("解除しました。");
			edgesns.framework.process("/showUserPage?userId=" + stylefree.memcache.get("user", "currentUserId"));
    	});
    }
})(edgesns.framework);



$(document).ready(function(){
	edgesns.tab.setup();
	edgesns.framework.process("/showMyPage");
});



edgesns.tab = function() {
	var my = {};
	var that = {};
	my.userSelect = true;
	that.setup = function() {
		$('#tabs').tabs();
		$('#tabs').bind('tabsselect', function(event, ui) {
			stylefree.logging.debug("ui.index" + ui.index + ":my.userSelect" + my.userSelect);
			//alert("2my.userSelect:" + my.userSelect);
			if(my.userSelect) {
				if(ui.index == 0) {
					if(stylefree.memcache.get("user", "currentUserId") == null || 
							stylefree.memcache.get("user", "userId") == stylefree.memcache.get("user", "currentUserId")) {
						edgesns.framework.process("/showMyPage");
					} else {
						edgesns.framework.process("/showUserPage?userId=" + stylefree.memcache.get("user", "currentUserId"));
					}
				} else if(ui.index == 1) {
					edgesns.framework.process("/showDiaryPage");
				} else if(ui.index == 2) {
					edgesns.framework.process("/showSearchPage");
				} else if(ui.index == 3) {
					edgesns.framework.process("/showProfileEditor");
				} else if(ui.index == 4) {
					
					var escapeHtmlTag = function(string) {
						return string.replace(/[<>]/g, function(match) {return {'<' : '＜','>' : '＞'}[match];});
					}		
					var logMessages = stylefree.logging.getLogMessages();
					var message = "";
					for(var i = 0; i < logMessages.length; i++) {
						message += escapeHtmlTag(logMessages[i]) + "\n";
					}
					$("#log_area").val(message);	
				}
			} else {
			}
			my.userSelect = true;
			//alert("3my.userSelect:" + my.userSelect);
		});
	}
	that.select = function(no) {
		//alert("1my.userSelect:" + my.userSelect);
		my.userSelect = false;
		$('#tabs').tabs('select', no);
		my.userSelect = true;
	}
	return that;
}();


